package cn.edu.nju.myweibo.activity;

import java.io.IOException;

import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.api.StatusesAPI;
import com.weibo.sdk.android.net.RequestListener;

import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;

@SuppressLint("HandlerLeak")
public class DeliverActivity extends Activity {

	private Handler handler = new Handler() {
		
		@Override
		public void handleMessage(Message msg) {
			boolean success = msg.getData().getBoolean("success");
			if (success) {
				Toast.makeText(DeliverActivity.this, "发送成功", Toast.LENGTH_SHORT).show();
				Intent intent = new Intent(DeliverActivity.this, MainActivity.class);
				startActivity(intent);
			} else {
				Toast.makeText(DeliverActivity.this, "发送失败", Toast.LENGTH_SHORT).show();
			}
		}
		
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_deliver);
		setListener();
	}
	
	private void setListener() {
		
		ImageButton back = (ImageButton) findViewById(R.id.back);
		back.setOnTouchListener(new OnTouchListener(){     
			@Override    
			public boolean onTouch(View v, MotionEvent event) {     
				if(event.getAction() == MotionEvent.ACTION_DOWN){   
					v.setBackgroundColor(getResources().getColor(R.color.touch));  
				}else if(event.getAction() == MotionEvent.ACTION_UP){     
					v.setBackgroundColor(getResources().getColor(R.color.theme));
					Intent intent = new Intent(DeliverActivity.this, MainActivity.class);
					startActivity(intent);
				}
				return false;     
			}
		});

		ImageButton submit = (ImageButton) findViewById(R.id.submit);
		submit.setOnTouchListener(new OnTouchListener(){     
			@Override    
			public boolean onTouch(View v, MotionEvent event) {     
				if(event.getAction() == MotionEvent.ACTION_DOWN){   
					v.setBackgroundColor(getResources().getColor(R.color.touch));  
				}else if(event.getAction() == MotionEvent.ACTION_UP){     
					v.setBackgroundColor(getResources().getColor(R.color.theme));
					EditText weibo_content = (EditText) findViewById(R.id.weibo_content);
					String content = weibo_content.getText().toString();
					LocationManager manager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
			        Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
					String latitude = "";
					String longtitude = "";
					if (location != null) {
						latitude = location.getLatitude() + "";
						longtitude = location.getLongitude() + "";
					}
					StatusesAPI api = new StatusesAPI(MainActivity.accessToken);
					api.update(content, latitude, longtitude, new RequestListener() {

						@Override
						public void onComplete(String response) {
							// TODO Auto-generated method stub
							Bundle bundle = new Bundle();
							bundle.putBoolean("success", true);
							Message message = new Message();
							message.setData(bundle);
							handler.sendMessage(message);
						}

						@Override
						public void onIOException(IOException e) {
							// TODO Auto-generated method stub
							Bundle bundle = new Bundle();
							bundle.putBoolean("success", false);
							Message message = new Message();
							message.setData(bundle);
							handler.sendMessage(message);
						}

						@Override
						public void onError(WeiboException e) {
							// TODO Auto-generated method stub
							Bundle bundle = new Bundle();
							bundle.putBoolean("success", false);
							Message message = new Message();
							message.setData(bundle);
							handler.sendMessage(message);
						}
						
					});
				}
				return false;     
			}
		});
		
	}

}
